package com.elinkway.infinitemovies.view;

import android.support.v7.util.DiffUtil;
import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.AlbumHeaderShowBean;
import com.elinkway.infinitemovies.bean.AlbumShowBean;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.google.gson.Gson;

import java.util.List;

/**
 * Author: liuxiaojiang(xiao)
 * Date: 2017-08-05
 * Time: 15:41
 * Description: FIXME
 */
public class DiffCallBack extends DiffUtil.Callback{
    private List<Object> mOldDatas, mNewDatas;
    private Gson gson = new Gson();

    public DiffCallBack(List<Object> mOldDatas, List<Object> mNewDatas) {
        this.mOldDatas = mOldDatas;
        this.mNewDatas = mNewDatas;
    }
    @Override
    public int getOldListSize() {
        return mOldDatas != null ? mOldDatas.size() : 0;
    }

    @Override
    public int getNewListSize() {
        return mNewDatas != null ? mNewDatas.size() : 0;
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        boolean isSame = false;
        Object beanOld = mOldDatas.get(oldItemPosition);
        Object beanNew = mNewDatas.get(newItemPosition);
        if(beanOld instanceof List && beanNew instanceof List){
            isSame = true;
        }
        if(beanOld instanceof AlbumShowBean && beanNew instanceof AlbumShowBean){
            if(gson.toJson(beanOld).equals(gson.toJson(beanNew))){
                isSame = true;
            }
        }
        if(beanOld instanceof AlbumHeaderShowBean && beanNew instanceof AlbumHeaderShowBean){
            if(gson.toJson(beanOld).equals(gson.toJson(beanNew))){
                isSame = true;
            }
        }
        return isSame;
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        boolean isSame = false;
        Object beanOld = mOldDatas.get(oldItemPosition);
        Object beanNew = mNewDatas.get(newItemPosition);
        if(beanOld instanceof List && beanNew instanceof List){
            isSame = true;
        }
        if(beanOld instanceof AlbumShowBean && beanNew instanceof AlbumShowBean){
            if(gson.toJson(beanOld).equals(gson.toJson(beanNew))){
                isSame = true;
            }
        }
        if(beanOld instanceof AlbumHeaderShowBean && beanNew instanceof AlbumHeaderShowBean){
            if(gson.toJson(beanOld).equals(gson.toJson(beanNew))){
                isSame = true;
            }
        }
        return isSame;
    }
}
